package edu.boun.g4.coursity.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.boun.g4.coursity.domain.Comment;
import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.CoursityUser;
import edu.boun.g4.coursity.domain.Rating;

/**
 * @author sebnemarslan
 * @author goksugurkas
 * @author mertusta
 * 
 */

@Service("commentService")
@Transactional
public class CommentService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * Retrieves all comments
	 * 
	 * @return a list of comments
	 */
	@SuppressWarnings("unchecked")
	public List<Comment> getAll() {
		logger.debug("Retrieving all comments");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM COMMENT");

		// Retrieve all
		return query.list();
	}
	
	/**
	 * Retrieves comments of a course
	 */
	public List<Comment> getAll(Integer courseId) {
		logger.debug("Retrieving comments of the course");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session
				.createQuery("FROM Course as p LEFT JOIN FETCH  p.comments WHERE p.id="
						+ courseId);

		Course course = (Course) query.uniqueResult();

		// Retrieve all
		return new ArrayList<Comment>(course.getComments());
	}

	/**
	 * Retrieves all comments
	 * 
	 * @return a list of comments by userId
	 */
	public List<Comment> getAllByUser(Integer userId) {
		logger.debug("Retrieving all ratings");

		// Retrieve session from Hibernate
				Session session = sessionFactory.getCurrentSession();

				// Create a Hibernate query (HQL)
				Query query = session
						.createQuery("FROM CoursityUser as p LEFT JOIN FETCH  p.comments WHERE p.id="
								+ userId);

				CoursityUser coursityUser = (CoursityUser) query.uniqueResult();

				// Retrieve all
				return new ArrayList<Comment>(coursityUser.getComments());
	}
	
	

	/**
	 * Retrieves a single comment
	 */
	public Comment get(Integer id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve an existing comment first
		Comment comment = (Comment) session.get(Comment.class, id);

		return comment;
	}

	/**
	 * Adds a new comment
	 */
	public void add(Integer courseId, Integer userId, Comment newComment) {
		logger.debug("Adding new comment");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Persists to db
		session.save(newComment);

		// Add to course as well
		// Retrieve existing course via id
		Course existingCourse = (Course) session.get(Course.class, courseId);

		// Assign updated values to this course
		existingCourse.getComments().add(newComment);

		// Save updates
		session.save(existingCourse);

		// Add to user as well
		// Retrieve existing user via id
		//CoursityUser existingUser = (CoursityUser) session.get(
		//		CoursityUser.class, userId);

		// Assign updated values to this user
		//existingUser.getComments().add(newComment);

		//session.save(existingUser);

	}

	/**
	 * Deletes an existing comment
	 * 
	 * @param id
	 *            the id of the existing comment
	 */
	public void delete(Integer id) {
		logger.debug("Deleting an existing comment");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing comment first
		Comment existingComment = (Comment) session.get(Comment.class, id);

		// Delete
		session.delete(existingComment);
	}

	/**
	 * Edits an existing comment
	 */
	public void edit(Comment updatedComment) {
		logger.debug("Editing an existing comment");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve an existing comment via id
		Comment existingComment = (Comment) session.get(Comment.class,
				updatedComment.getId());

		// Assign updated values to this comment
		existingComment.setCommentMessage(updatedComment.getCommentMessage());
		existingComment.setCommentDate(updatedComment.getCommentDate());

		// Save updates
		session.save(existingComment);
	}

}
